/**
 * Created by wangzilong on 2017/4/11.
 */
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Image,
    ScrollView,
    TouchableOpacity,
    TextInput,
    Navigator,
    Platform,
    NativeModules
} from 'react-native';
import BarcodeScanner from 'react-native-barcodescanner';
import shopService from '../lib/services/shopService';
import StorageUser from '../lib/plugins/StorageUser';
import { NavigationActions } from 'react-navigation';
import LoadingModal from '../components/LoadingModal';
var Dimensions = require('Dimensions');
const { width, height } = Dimensions.get('window');
export default class ScanCode extends Component{
    constructor(props) {
        super(props);
        this.data = {};
        // alert(this.props.navigation.state.params.userId)
        this.state = {
            torchMode: 'off',
            cameraType: 'back',
            userId:this.props.navigation.state.params.userId,
            token:this.props.navigation.state.params.token,
            cameraType: 'back',
            torchMode: 'off',
            load:false,
        };
    }
    render(){
        return (
            <View style={styles.container}>
                <LoadingModal visible={this.state.load} onRequestClose={()=>{console.log('666')}}/>
                <BarcodeScanner
                    onBarCodeRead={this.barcodeReceived.bind(this)}
                    style={styles.boxView}
                    torchMode={this.state.torchMode}
                    cameraType={this.state.cameraType}
                    withshowViewFinder={false}
                />
                <View style={styles.topBar}>

                </View>
                <View style={styles.rightBar}>

                </View>
                <View style={styles.leftBar}>
                </View>
                <View style={styles.BottomBar}>
                </View>
                <TouchableOpacity onPress={()=>this.goBack()} style={{justifyContent:'flex-end'}}>
                    <View style={styles.textView}>
                        <Text style={{color:'#fff',fontSize:16}}>取消</Text>
                    </View>
                </TouchableOpacity>
            </View>
        )
    }
    barcodeReceived(e) {
        var $this = this;
        console.log('Barcode: ' + e.data);
        console.log('Type: ' + e.type);
        // alert(JSON.stringify(e))
        if($this.data.data){
            return;
        }else{
            $this.data = e;
            this.setState({
                load:true
            })
            shopService.shopUserScanCode(function(data){
                // alert(JSON.stringify(data));
                if(data.code==10000){
                    $this.setState({
                        load:false
                    })
                    const resetAction = NavigationActions.reset({
                        index: 1,
                        actions: [
                            NavigationActions.navigate({ routeName: 'homePage'}),
                            NavigationActions.navigate({ routeName: 'HistoryList',params:{dataList:data.data}})
                        ]
                    })
                    $this.props.navigation.dispatch(resetAction)
                }
            },$this.state.userId,e.data,$this.state.token)
        }
    }
    goBack(){
        this.props.navigation.goBack();
    }

}

const styles = StyleSheet.create({
    container:{
        flex:1,
        position:'relative'
    },
    boxView:{
        flex:1,
    },
    textView:{
        alignItems:'center',
        justifyContent:'center',
        height:50,
        width:width,
        backgroundColor:'rgb(79,145,237)'
    },
    topBar: {
        width:width,
        height: (height-276)/2,
        alignItems: 'center',
        justifyContent: 'center',
        position:'absolute',
        top:0,
        left:0,
        backgroundColor:'rgba(0,0,0,.4)'
    },
    leftBar: {
        width:width/2-100,
        height: 200,
        alignItems: 'center',
        justifyContent: 'center',
        position:'absolute',
        top:(height-276)/2,
        left:0,
        backgroundColor:'rgba(0,0,0,.4)'
    },
    rightBar:{
        width:width/2-100,
        height: 200,
        alignItems: 'center',
        justifyContent: 'center',
        position:'absolute',
        top:(height-276)/2,
        right:0,
        backgroundColor:'rgba(0,0,0,.4)'
    },
    BottomBar:{
        width:width,
        height: height/2-88,
        alignItems: 'center',
        justifyContent: 'center',
        position:'absolute',
        bottom:0,
        left:0,
        backgroundColor:'rgba(0,0,0,.4)'
    }, 
});